package org.jsping.music;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsping.entity.Music;
import org.jsping.service.impl.MusicServiceImpl;

/**
 * 音乐爬虫到的歌曲有时效性,所以可能隔几天就失效,用户在播放歌曲的时候如果不能正常会发送这个请求来进行处理
 * 将歌曲需要进行更新的操作,最后将其更新到数据库中
 * 更新音乐  根据id和歌曲传过来的路径来进行更新
 */
@WebServlet("/UpdateMusic")
public class UpdateMusic extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public UpdateMusic() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String mid = request.getParameter("mid");
		//传送过来的数据中带了&,所以在发送请求的时候将其替换成了
		String murl = request.getParameter("murl");     
			   murl = murl.replaceAll("abc123", "&");
		if(mid!=null) {
			int num = new MusicServiceImpl().updateMusic(new Integer(mid), murl);
			/*更新成功就去更新缓存中的数据*/
			if(num>0) {
				Music music = new MusicServiceImpl().getMusicInfoByURl(murl);
				CacheMusic.getInstance().musiclist.put(mid, music);  //覆盖掉之前缓存的数据
			}
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
